import Cookies from 'js-cookie';
// import router from '@/router'
import {getStore, setStore, removeStore} from '@/common/utils';
import iView from 'iview';
import apiList from '@/api/api';

export default {
    state: {
        token: '', // 令牌
        userInfo: {}, // 用户信息
        dept: {}, // 部门信息
        role: {}, // 角色
        org: {}, // 组织信息
        accesslist: [], // 权限树
        menu: [], // 可用菜单权限
        isLogin: !!getStore('token'), // 登录状态，根据令牌生效判断
        captchaUrl: '', // 登录页验证码
        cptId: '', // 登录页验证码id
        msg: '', // 消息
        headerNeeded: true,
        menuNeeded: true,
        isLoginPage: false,
        mobileMode: false,
        menuCollapse: true,
        toggle: true,
        posts: [],
        roomid: '',
        socket: null,
        wsStatus: 0,
        onlineCount: 0,
        onlineUsers: [],
        msgArr: [],
        sidebar: {
            opened: !+Cookies.get('sidebarStatus'),
            minOpen: false // 小屏时菜单状态
        },
        router: {
            currentPageName: 'index'
        },
        lang: {
            icon: '#icon-zhongguo',
            type: 'CN'
        },
        searchState: {
            show: false,
            params: ''
        }
    },
    mutations: {
        /**
         * 菜单的缩展
         */
        TOGGLE_SIDEBAR: state => {
            let winWidth = document.documentElement.clientWidth;
            if (winWidth <= 600) {
                state.sidebar.minOpen = !state.sidebar.minOpen;
                state.sidebar.opened = false;
                Cookies.set('sidebarStatus', 0);
            } else {
                if (state.sidebar.opened) {
                    Cookies.set('sidebarStatus', 1);
                } else {
                    Cookies.set('sidebarStatus', 0);
                }
                state.sidebar.opened = !state.sidebar.opened;
            }
        },
        CLOSE_SLIDEBAR: state => {
            Cookies.set('sidebarStatus', 0);
            state.sidebar.opened = false;
            state.sidebar.minOpen = false;
        },
        OPEN_SLIDEBAR: state => {
            Cookies.set('sidebarStatus', 1);
            state.sidebar.opened = true;
        },
        /**
         * 面包屑
         */
        GET_CURRENT_PAGE_NAME: (state, payload) => {
            state.router.currentPageName = payload;
        },
        /**
         * 设置语言
         */
        SET_LANG: (state, payload) => {
            state.lang = payload;
            Cookies.set('lang', payload);
        },
        /**
         * 搜索过滤
         */
        SEARCH_FILTER: (state, payload) => {
            if (!payload.params.meta.search) { // 路由元信息设置是否有搜索模块
                iView.Message.error('该页不提供搜索!');
            } else {
                Object.assign(state.searchState, {
                    show: !state.searchState.show
                }, payload);
            }
        },
        SET_TOKEN: (state, value) => {
            state.token = value;
            state.isLogin = true;
            setStore('token', state.token);
        },
        SET_USER: (state, value) => {
            state.token = value.token;
            state.userInfo = value.userInfo;
            state.role = value.role;
            state.org = value.org;
            state.dept = value.dept;
            state.menu = value.menu;
            state.accesslist = value.accesslist;
            state.isLogin = true;
        },
        LOGOUT: (state) => {
            state.token = null;
            removeStore('token');
            state.userInfo = null;
            state.isLogin = false;
        }
    },
    actions: {
        ToggleSideBar: ({
                            commit
                        }) => {
            commit('TOGGLE_SIDEBAR');
        },
        getCurrentPageName: ({
                                 commit
                             }, payload) => {
            commit('GET_CURRENT_PAGE_NAME', payload);
        },
        setLang: ({
                      commit
                  }, payload) => {
            commit('SET_LANG', payload);
        },
        searchFilter: ({
                           commit
                       }, payload) => {
            commit('SEARCH_FILTER', payload);
        },
        getUserInfo: ({
                          commit
                      }, payload) => {
            apiList.getMe().then((response) => {
                // console.log("getme!!",response)
                return commit('SET_USER', {
                    userInfo: response.Data.userinfo,
                    role: response.Data.role,
                    org: response.Data.org,
                    dept: response.Data.dept,
                    token: response.token,
                    menu: response.Data.menu,
                    accesslist: response.Data.accesslist.children
                });
            });
        }
    }
};

